﻿using DSTMEETING.Win.App.Activities;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DSTMEETING.Win.App.Views
{
    /// <summary>
    /// MainView.xaml 的交互逻辑
    /// </summary>
    public partial class MainView : UserControl
    {
        public MainView()
        {
            InitializeComponent();

            s_mainViewRef = new WeakReference<MainView>(this);

            popContainer.Visibility = Visibility.Hidden;
            //popContainer.HorizontalContentAlignment = HorizontalAlignment.Center;
            //popContainer.VerticalContentAlignment = VerticalAlignment.Center;
            ctlFrame.Switch(new MainActivity());
        }
        
        private void ctlJoin_Click(object sender, RoutedEventArgs e)
        {
            ctlFrame.Switch(new SelectMeetingActivity());
        }

        private void btnBack_Click(object sender, RoutedEventArgs e)
        {
            ctlFrame.Back();
        }


        private void ClearToolbarItems()
        {
            toolbar.Children.Clear();
        }
        private void AddToolbarItem(UIElement item)
        {
            toolbar.Children.Add(item);
        }

        #region static methods
        private static WeakReference<MainView> s_mainViewRef;
        private static Action<bool, object> s_popupClosedFunc;
        public static void SetHeader(object title, UIElement toolbar)
        {
            MainView mainView = null;
            if (s_mainViewRef.TryGetTarget(out mainView))
            {
                mainView.title.Content = title;
                mainView.ClearToolbarItems();
                mainView.AddToolbarItem(toolbar);
            }
        }

        public static void ShowPopup(object title, UIElement content, Action<bool, object> closedFunc)
        {
            MainView mainView = null;
            if (s_mainViewRef.TryGetTarget(out mainView))
            {
                s_popupClosedFunc = closedFunc;
                mainView.popHeader.Content = title;
                mainView.popBorder.Child = content;
                mainView.popContainer.Visibility = Visibility.Visible;
            }
        }
        public static void ClosePopup(UIElement content, bool result, object data)
        {
            MainView mainView = null;
            if (s_mainViewRef.TryGetTarget(out mainView))
            {
                if(mainView.popBorder.Child == content)
                {
                    mainView.popContainer.Visibility = Visibility.Hidden;
                    mainView.popBorder.Child = content;
                    s_popupClosedFunc.Invoke(result, data);
                }
            }
        }
        #endregion

        private void btnClosePopup_Click(object sender, RoutedEventArgs e)
        {
            MainView.ClosePopup(this.popBorder.Child, false, null);
        }
    }
}
